<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\doctor\controller;

use think\Db;
use cmf\controller\RestBaseController;
use think\Config;
use think\Cache;
use \Qcloud\sms\SmsSingleSender AS sms;

class OrderController extends RestBaseController
{

    
    public $data;
    public $bizid;
    public $type;
    public $tel;
    public $limit = 10;
    public $doc_id;

    public function __construct(){
        $token =  input('token') ? input('token') : $_SERVER['HTTP_TOKEN'];
        $appid =  input('appid') ? input('appid') : $_SERVER['HTTP_APPID'];
        if(!$token){
            $this->error('缺少:token');
        }
        if(!Cache::get($token)){
            echo json_encode(array('code'=>90001,'msg'=>'token已过期'));die;
        }
        preg_match_all('/\d+/',$token,$arr);
        if(count(@$arr[0]) != 3){
            $this->error('token:有误!');
        }
        if($arr[0][2] == 2){//如果是医生
                $bizid = Db::prefix_close()->table('doctor')
                    ->where(['tel'=>$arr[0][1]])
                    ->field('biz_id,id')
                    ->find();
            $doc_id = $bizid['id'];
            $bizid = $bizid['biz_id'];
            $this->type= 2;
        }else{//如果是助手 就只返回他的医生的数据
            $hep = Db::prefix_close()->table('helper')
                    ->where(['tel'=>$arr[0][1]])
                    ->field('biz_id,doc_id')
                    ->find();
            $bizid = $hep['biz_id'];
            $doc_id = $hep['doc_id'];
            $this->type = 3;
        }
        $this->doc_id = !isset($doc_id) ? '' : 'c.id = '.$doc_id;
        $this->bizid = $bizid;
        $this->data = input();
    }
   
    /**
     * [index 我的订单]
     * @return [type] [description]
     */
    public function index(){
        if(!isset($this->data['page'])||empty($this->data['page'])||$this->data['page']<=0){
            $this->data['page'] = 1;
        }
        if (!isset($this->data['limit'])||empty($this->data['limit'])) {
            $this->data['limit'] = $this->limit;
        }
        $limit = $this->data['limit'];
        $pages = ($this->data['page']-1)*$limit;
        $data = Db::prefix_close()->table('miniorder')
            ->alias('a')
            ->join('miniuser b','b.openid=a.openid','left')
            ->join('doctor c','c.id=a.doctorid','left')
            ->field('c.name,a.orderinfo,a.orderid,a.createtime,a.ordertype,a.remark,a.price')
            ->where(['a.bizid'=>$this->bizid])
            ->where($this->doc_id)
            ->limit($pages,$limit)
            ->order('a.createtime desc')
            ->select();
        $newdata = [];
        foreach ($data as $key => $value) {
            $orderinfo = json_decode($value['orderinfo'],true);
            $newdata[$key]['name'] = $value['name'];
            $newdata[$key]['hospital'] = $orderinfo['poi_name'];
            $newdata[$key]['orderid'] = $value['orderid'];
            $newdata[$key]['createtime'] = $value['createtime'];
            $newdata[$key]['service_type'] = $value['ordertype'];
            $newdata[$key]['servicename'] = $value['remark'];
            $newdata[$key]['timeStr'] = strtotime($value['createtime']);
            $newdata[$key]['price'] = $value['price'];
        }
        $this->success('请求成功！',$newdata);
    }
}